<?php
namespace app\api\controller;
use think\Controller;
use think\Db;
use think\Request;
use think\View;
Class Jisi extends Index{
	public function __construct(Request $request)
	{
		parent::__construct($request);
	}
	/**
	* 公共祭祀
	*/
	public function jisilist()
	{
		$where['jisitype']=input('jisitype');
		$page=Db::name('jisi')->where(['jisitype'=>'0'])->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
		$jisilist=$page->all();
		foreach ($jisilist as $key => $value) {
			$jisilist[$key]['nickname']=Db::name('user')->where(['userid'=>$jisilist[$key]['userid']])->value('nickname');
		}
		$this->assign('title','公共祭祀');
		$this->assign('page',$page);
		// $this->assign('where',$where);
		$this->assign('jisilist',$jisilist);
		return view('jisilist');
	}
	/**
	* 私人祭祀
	*/
	public function jisilistPri(){
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			$where['userid']=session('userid');
			$page = Db::name('jisi')->where($where)->paginate(6,false,['type' => 'bootstrap','var_page' => 'page']);
			$jisilistPri = $page->all();
			foreach ($jisilistPri as $key => $value) {
				$jisilistPri[$key]['nickname'] = Db::name('user')->where(['userid'=>$jisilistPri[$key]['userid']])->value('nickname');
			}
			$this->assign('title','私人祭祀');
			$this->assign('page',$page);
			$this->assign('jisilistPri',$jisilistPri);
			// $this->assign('where',$where);
			return view('jisilistPri');
		}
	}
	/**
	* 祭祀跳转
	*/ 
	public function jisi()
	{
		$where['jisiid']=input('jisiid');
		$jisi=Db::name('jisi')->where($where)->find();
		$jisi['nickname']=Db::name('user')->where(['userid'=>$jisi['userid']])->value('nickname');
		//所有物品
		$wupin=Db::name('JisiWupin')->select();
		//统计当前祭祀的每项物品的和
		$jisinum=Db::name('JisiDetail')->where($where)->group('wupinid')->select();
		//祭祀详情人员头像
		$jisiphoto=Db::name('JisiDetail')->where($where)->group('userid')->select();
		foreach($jisiphoto as $key => $value){
			$jisiphoto[$key]['photo']=Db::name('user')->where(['userid'=>$jisiphoto[$key]['userid']])->value('photo');
		}
		// 遍历当前分组的数据
		foreach ($wupin as $key => $value) {
			//根据物品id查询改物品的和。
			$where2['jisiid']=input('jisiid');
			$where2['wupinid']=$wupin[$key]['wupinid'];
			$wupin[$key]['sum']=Db::name('JisiDetail')->where($where2)->sum('num');
			// 判断为空或者是‘’
			if($wupin[$key]['sum']==null || $wupin[$key]['sum']==''){
				$wupin[$key]['sum']=0;
			}
		}

		$this->assign('jisi',$jisi);
		$this->assign('jisiphoto',$jisiphoto);
		$this->assign('wupin',$wupin);
		return view('jisi');
	}
	/**
	* 祭祀物品统计
	*/
	public function jisiWupin()
	{
		if(session('userid')==''){
			res('log','去登陆');
			return false;
		}
		//插入活跟新的数据
		$data['userid']=session('userid');
		$data['wupinid']=input('post.wupinid');
		$data['jisiid']=input('post.jisiid');
		$data['jisitime']=time();
		$data['num']=1;
		// 根据当前登陆人员查询是否已经添加过对应的祭祀物品
		$where['userid']=session('userid');
		$where['jisiid']=input('post.jisiid');
		$where['wupinid']=input('post.wupinid');
		$jsuser='';
		$user='';
		$jd=Db::name('JisiDetail')->where($where)->find();
		if($jd){
			$jdset=Db::name('JisiDetail')->where($where)->setInc('num',1);
		}else{
			$where2['userid']=session('userid');
			$where2['jisiid']=input('post.jisiid');
			$jsuser=Db::name('JisiDetail')->where($where2)->find();
			if($jsuser==''|| $jsuser== null){
				$user=Db::name('user')->where(['userid'=>session('userid')])->value('photo');
			}
			$jdset=Db::name('JisiDetail')->insert($data);
		}
		if($jdset){
			res('success','祭祀成功',$user);
		}else{
			res('error','请重试');
		}
	} 
	/**
	* 发起祭祀
	*/
	public function jisiAdd()
	{
		if(!session('userid')){
			jsBack('您还未登录,前去登陆','/api/register/index');
			return false;
		}else{
			return view('jisiadd');
		}
		
	} 
	public function jisiInsert()
	{
		if(request()->isPost()){
			if(input('post.jisiname')=="" || input('post.jisicontent')==''){
				jsBack('标题和内容不可以为空');
				return false;
			}
			//上传图片
			$file = request()->file('jisiphoto');
			if($file){
			    // 移动到框架应用根目录/public/uploads/jisi 目录下
		        $info = $file->validate(['size'=>2*1024*1024,'ext'=>'jpg,png,gif'])->move(ROOT_PATH . 'public' . DS . 'uploads/jisi');
		        //上传成功后获取传记信息
		        if($info){
		        	$data['jisiphoto']='/public/uploads/jisi/'.date("Ymd").'/'.$info->getFilename();
			        $data['jisiname']=input('post.jisiname');
					$data['jisicontent']=input('post.jisicontent');
					//私人祭祀类型1 公开类型0
					// $data['jisitype']=1;
					$data['addtime']=time();
					$data['userid']=session('userid');
					$zhuanji=Db::name('jisi')->insertGetId($data);
					//传记添加
					if($zhuanji){
						jsBack('添加成功','/api/jisi/jisilist?jisitype=1');
					}else{
						jsBack('网络有误,请重试');
					}
		        }else{//输出错误信息
		        	jsBack($file->getError());
		            return false;
		        }
		    }else{
		    	jsBack('请选择传记展示图');
		    }
		}else{
			//如果不是post跳转会添加页
			$this->redirect('index/index');
		}
	} 
}